﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;

namespace app.Api
{

    #region 辅助类
    static class ConvertEx
    {
        public static List<T> DataTableToList<T>(DataTable dt) where T : class, new()
        {
            // 首先找到所有可以设置的属性，判断属性是否可写入，是否包含在datatable中
            PropertyInfo[] properties = typeof(T).GetProperties();// 获得此模型的公共属性
            List<PropertyInfo> writable_properties = new List<PropertyInfo>();
            foreach (PropertyInfo pi in properties)
            {
                if (pi.CanWrite && dt.Columns.Contains(pi.Name)) writable_properties.Add(pi);
            }

            // 构造列表
            List<T> ts = new List<T>();// 定义集合
            foreach (DataRow dr in dt.Rows)
            {
                T t = new T();
                foreach (PropertyInfo pi in writable_properties)
                {
                    object value = dr[pi.Name];
                    if (value != DBNull.Value)
                    {
                        pi.SetValue(t, ChangeType((IConvertible)value, pi.PropertyType), null);
                    }
                }
                ts.Add(t);
            }
            return ts;
        }

        public static object ChangeType(this IConvertible convertibleValue, Type t)
        {
            //if (string.IsNullOrEmpty(convertibleValue.ToString()))
            //{
            //    return default(T);
            //}
            if (!t.IsGenericType)
            {
                return Convert.ChangeType(convertibleValue, t);
            }
            else
            {
                Type genericTypeDefinition = t.GetGenericTypeDefinition();
                if (genericTypeDefinition == typeof(Nullable<>))
                {
                    return Convert.ChangeType(convertibleValue, Nullable.GetUnderlyingType(t));
                }
            }
            throw new InvalidCastException(string.Format("Invalid cast from type \"{0}\" to type \"{1}\".", convertibleValue.GetType().FullName, t.FullName));
        }
    }
    static class DateTimeEx
    {
        // 将时间转为时间戳
        public static string DateTimeToTimeStamp(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
            long t = (time.Ticks - startTime.Ticks) / 10000;   //除10000调整为13位      
            return t.ToString();
        }
        public static string GetNowTimeStamp()
        {
            return DateTimeToTimeStamp(DateTime.Now);
        }
        //将时间戳转换成时间
        public static DateTime TimeStampToDatetime(string timestamp)
        {
            long timeStamp = long.Parse(timestamp);
            DateTime defaultTime = new DateTime(1970, 1, 1, 0, 0, 0);
            long defaultTick = defaultTime.Ticks;
            long timeTick = defaultTick + timeStamp * 10000;
            //// 东八区 要加上8个小时
            DateTime dt = new DateTime(timeTick).AddHours(8);
            return dt;

        }
    }
    #endregion
}